package com.ruibang.glass.file.controller;

import com.ruibang.glass.common.domain.file.FileBusiness;
import com.ruibang.glass.file.domain.req.FileInfoReq;
import com.ruibang.glass.file.domain.req.PreviewFileReq;
import com.ruibang.glass.file.entity.FileInfo;
import com.ruibang.glass.file.service.FileService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/glass/file")
@Api(value = "文件接口", tags = {"文件接口"})
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping(value = "/uploadFile", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @ApiImplicitParam(name = "files",value = "上传文件到本地",required = true,dataType="MultipartFile",dataTypeClass = MultipartFile.class,paramType = "query")
    public ResultBody uploadFile(@RequestPart("files") MultipartFile files) {
        return ResultBody.success(fileService.uploadFile(files));
    }

    @PostMapping(value = "/uploadFiles",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "批量文件上传", notes = "批量文件上传")
    public ResultBody uploadFiles(@RequestPart("files") List<MultipartFile> files) {
        return ResultBody.success(fileService.uploadFiles(files));
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "保存文件信息", notes = "保存文件信息")
    public ResultBody save(@RequestBody List<FileInfo> files) {
        return ResultBody.success(fileService.saveBatch(files));
    }

    @PostMapping("/previewFiles")
    @ApiOperation(value = "文件批量预览", notes = "文件批量预览")
    public ResultBody previewFiles(@RequestBody PreviewFileReq previewFileReq) {
        return ResultBody.success(fileService.previewFiles(previewFileReq));
    }

    @PostMapping("/previewLocalFiles")
    @ApiOperation(value = "文件批量预览(本地预览)", notes = "文件批量预览(本地预览)")
    public ResultBody previewLocalFiles(@RequestBody List<String> fileIds) {
        return ResultBody.success(fileService.previewLocalFiles(fileIds));
    }

    @PostMapping("/previewFile")
    @ApiOperation(value = "文件预览", notes = "文件预览")
    public ResultBody previewFile(@RequestBody PreviewFileReq previewFileReq) {
        return ResultBody.success(fileService.previewFile(previewFileReq));
    }

    @PostMapping("/getPreviewByBusinessParam")
    @ApiOperation(value = "根据参数查询文件信息", notes = "根据参数查询文件信息")
    public ResultBody<List<String>> getPreviewByBusinessParam(@RequestBody FileBusiness fileBusiness) {
        return ResultBody.success(fileService.getPreviewByBusinessParam(fileBusiness));
    }

    @GetMapping("/delete/{fileId}")
    @ApiOperation(value = "删除文件", notes = "删除文件")
    public ResultBody delete(@PathVariable("fileId") String fileId) {
        fileService.deleteById(fileId);
        return ResultBody.success();
    }

    @PostMapping("/queryPage")
    @ApiOperation(value = "查询文件（分页）", notes = "查询文件（分页）")
    public ResultBody queryPage(@RequestBody FileInfoReq infoReq) {
        return ResultBody.success(fileService.queryPage(infoReq));
    }

    @PostMapping("/queryFiles")
    @ApiOperation(value = "查询文件列表", notes = "查询文件列表")
    public ResultBody queryFiles(@RequestBody FileInfo fileInfo) {
        return ResultBody.success(fileService.queryFiles(fileInfo));
    }

    @PostMapping("/updateBusinessById")
    @ApiOperation(value = "更新文件对应业务Id", notes = "更新文件对应业务Id")
    public ResultBody updateBusinessById(@RequestBody FileBusiness fileBusiness) {
        fileService.updateBusinessById(fileBusiness);
        return ResultBody.success();
    }

    @GetMapping("/deleteByBusinessId/{businessId}")
    @ApiOperation(value = "根据业务Id删除文件", notes = "根据业务Id删除文件")
    public ResultBody deleteByBusinessId(@PathVariable("businessId") String businessId) {
        fileService.deleteByBusinessId(businessId);
        return ResultBody.success();
    }


    @GetMapping("/getFileIdsByBusinessId/{businessId}")
    @ApiOperation(value = "根据业务Id查询文件Id列表", notes = "根据业务Id查询文件Id列表")
    public ResultBody<List<String>> getFileIdsByBusinessId(@PathVariable("businessId") String businessId) {
        return ResultBody.success(fileService.getFileIdsByBusinessId(businessId));
    }


    @PostMapping("/getFileBusinessByIds")
    @ApiOperation(value = "根据业务Id列表查询文件业务信息", notes = "根据业务Id列表查询文件业务信息")
    public ResultBody<List<FileBusiness>> getFileBusinessByIds(@RequestBody List<String> businessIds) {
        return ResultBody.success(fileService.getFileBusinessByIds(businessIds));
    }

    @PostMapping("/deleteByFileIds")
    @ApiOperation(value = "根据文件id列表删除业务对应文件", notes = "根据文件id列表删除业务对应文件")
    public ResultBody updateBusinessById(@RequestBody List<String> fileIds) {
        fileService.deleteByFileIds(fileIds);
        return ResultBody.success();
    }

    @GetMapping("/getDetails/{businessId}")
    @ApiOperation(value = "根据businessId获取文件信息", notes = "根据businessId获取文件信息")
    public ResultBody<FileInfo> getDetails(@PathVariable String businessId) {
        return ResultBody.success(fileService.getDetails(businessId));
    }

    @PostMapping("/getFileInfoByBusinessIds")
    @ApiOperation(value = "根据businessId批量获取获取文件信息", notes = "根据businessId批量获取获取文件信息")
    public ResultBody<List<FileInfo>> getFileInfoByBusinessIds(@RequestBody List<String> businessIds) {
        return ResultBody.success(fileService.getFileInfoByBusinessIds(businessIds));
    }

    @PostMapping("/getFileInfoByFileIds")
    @ApiOperation(value = "根据fileId批量获取获取文件信息", notes = "根据fileId批量获取获取文件信息")
    public ResultBody<List<FileInfo>> getFileInfoByFileIds(@RequestBody List<String> fileIds) {
        return ResultBody.success(fileService.getFileInfoByFileIds(fileIds));
    }

    @GetMapping("/downloadFile/{id}")
    @ApiOperation(value = "更具id下载文件", notes = "根据文件id列表删除业务对应文件")
    public void downloadFile(@PathVariable("id") String id, HttpServletResponse response) {
        fileService.downLoad(id, response);
    }

    @PostMapping("/deleteByBusinessIdAndAttribute")
    @ApiOperation(value = "根据业务Id删除文件", notes = "根据业务Id删除文件")
    public ResultBody deleteByBusinessIdAndAttribute(@RequestBody FileBusiness fileBusiness) {
        fileService.deleteByBusinessIdAndAttribute(fileBusiness);
        return ResultBody.success();
    }

    @GetMapping("/getFileInfo/{fileId}")
    @ApiOperation(value = "获取文件信息", notes = "获取文件信息")
    public ResultBody<FileInfo> getFileInfo(@PathVariable("fileId") String fileId) {
        return ResultBody.success(fileService.getById(fileId));
    }

}
